home *** CD-ROM | disk | FTP | other *** search
/ Visual Cafe 3 / Visual Cafe 3.ISO / Vcafe / Main.bin / TextArea.java < prev    next >
Text File  |  1998-09-22  |  14KB  |  468 lines

  1. /*
  2.  * @(#)TextArea.java    1.42 98/08/13
  3.  *
  4.  * Copyright 1995-1998 by Sun Microsystems, Inc.,
  5.  * 901 San Antonio Road, Palo Alto, California, 94303, U.S.A.
  6.  * All rights reserved.
  7.  * 
  8.  * This software is the confidential and proprietary information
  9.  * of Sun Microsystems, Inc. ("Confidential Information").  You
  10.  * shall not disclose such Confidential Information and shall use
  11.  * it only in accordance with the terms of the license agreement
  12.  * you entered into with Sun.
  13.  */
  14. package java.awt;
  15.  
  16. import java.awt.peer.TextAreaPeer;
  17.  
  18. /**
  19.  * A <code>TextArea</code> object is a multi-line region 
  20.  * that displays text. It can be set to allow editing or 
  21.  * to be read-only.
  22.  * <p>
  23.  * The following image shows the appearance of a text area:
  24.  * <p>
  25.  * <img src="images-awt/TextArea-1.gif" 
  26.  * ALIGN=center HSPACE=10 VSPACE=7>
  27.  * <p>
  28.  * This text area could be created by the following line of code:
  29.  * <p>
  30.  * <hr><blockquote><pre>
  31.  * new TextArea("Hello", 5, 40);
  32.  * </pre></blockquote><hr>
  33.  * <p>
  34.  * @version    1.42, 08/13/98
  35.  * @author     Sami Shaio
  36.  * @since       JDK1.0
  37.  */
  38. public class TextArea extends TextComponent {
  39.  
  40.     /**
  41.      * The number of rows in the TextArea.
  42.      */
  43.     int    rows;
  44.  
  45.     /**
  46.      * The number of columns in the TextArea.
  47.      */
  48.     int    columns;
  49.  
  50.     private static final String base = "text";
  51.     private static int nameCounter = 0;
  52.  
  53.     /**
  54.      * Create and display both vertical and horizontal scrollbars.
  55.      * @since JDK1.1
  56.      */
  57.     public static final int SCROLLBARS_BOTH = 0;
  58.  
  59.     /**
  60.      * Create and display vertical scrollbar only.
  61.      * @since JDK1.1
  62.      */
  63.     public static final int SCROLLBARS_VERTICAL_ONLY = 1;
  64.  
  65.     /**
  66.      * Create and display horizontal scrollbar only.
  67.      * @since JDK1.1
  68.      */
  69.     public static final int SCROLLBARS_HORIZONTAL_ONLY = 2;
  70.  
  71.     /**
  72.      * Do not create or display any scrollbars for the text area.
  73.      * @since JDK1.1
  74.      */
  75.     public static final int SCROLLBARS_NONE = 3;
  76.  
  77.     /**
  78.      * Determines which scrollbars are created for the 
  79.      * text area.
  80.      */
  81.     private int scrollbarVisibility;
  82.  
  83.     /*
  84.      * JDK 1.1 serialVersionUID 
  85.      */
  86.      private static final long serialVersionUID = 3692302836626095722L;
  87.  
  88.     /**
  89.      * Constructs a new text area.
  90.      * This text area is created with both vertical and
  91.      * horizontal scroll bars.
  92.      * @since     JDK1.0
  93.      */
  94.     public TextArea() {
  95.     this("", 0, 0, SCROLLBARS_BOTH);
  96.     }
  97.  
  98.     /**
  99.      * Constructs a new text area with the specified text.
  100.      * This text area is created with both vertical and
  101.      * horizontal scroll bars.
  102.      * @param     text the text to be displayed. 
  103.      * @since     JDK1.0 
  104.      */
  105.     public TextArea(String text) {
  106.     this(text, 0, 0, SCROLLBARS_BOTH);
  107.     }
  108.  
  109.     /**
  110.      * Constructs a new empty TextArea with the specified number of
  111.      * rows and columns.
  112.      * @param rows the number of rows
  113.      * @param columns the number of columns
  114.      */
  115.     public TextArea(int rows, int columns) {
  116.     this("", rows, columns);
  117.     }
  118.  
  119.     /**
  120.      * Constructs a new text area with the specified text,
  121.      * and with the specified number of rows and columns.
  122.      * This text area is created with both vertical and
  123.      * horizontal scroll bars.
  124.      * @param     text      the text to be displayed.
  125.      * @param     rows      the number of rows.
  126.      * @param     columns   the number of columns.
  127.      * @since     JDK1.0
  128.      */
  129.     public TextArea(String text, int rows, int columns) {
  130.         this(text, rows, columns, SCROLLBARS_BOTH);
  131.     }
  132.  
  133.     /**
  134.      * Constructs a new text area with the specified text, 
  135.      * and with the rows, columns, and scroll bar visibility
  136.      * as specified. 
  137.      * <p>
  138.      * The <code>TextArea</code> class defines several constants
  139.      * that can be supplied as values for the
  140.      * <code>scrollbars</code> argument: 
  141.      * <code>SCROLLBARS_BOTH</code>, 
  142.      * <code>SCROLLBARS_VERTICAL_ONLY</code>, 
  143.      * <code>SCROLLBARS_HORIZONTAL_ONLY</code>, 
  144.      * and <code>SCROLLBARS_NEITHER</code>. 
  145.      * @param     text the text to be displayed.
  146.      * @param     rows the number of rows.
  147.      * @param     columns the number of columns.
  148.      * @param     scrollbars a constant that determines what 
  149.      *                scrollbars are created to view the text area.
  150.      * @since     JDK1.1
  151.      */
  152.     public TextArea(String text, int rows, int columns, int scrollbars) {
  153.     super(text);
  154.     this.rows = rows;
  155.     this.columns = columns;
  156.     this.scrollbarVisibility = scrollbars;
  157.     }
  158.  
  159.     /**
  160.      * Construct a name for this component.  Called by getName() when the
  161.      * name is null.
  162.      */
  163.     String constructComponentName() {
  164.         return base + nameCounter++;
  165.     }
  166.  
  167.     /**
  168.      * Creates the TextArea's peer.  The peer allows us to modify
  169.      * the appearance of the TextArea without changing any of its
  170.      * functionality.
  171.      */
  172.     public void addNotify() {
  173.       synchronized (getTreeLock()) {
  174.     if (peer == null)
  175.         peer = getToolkit().createTextArea(this);
  176.     super.addNotify();
  177.       }
  178.     }
  179.  
  180.     /**
  181.      * Inserts the specified text at the specified position
  182.      * in this text area.
  183.      * @param      str the text to insert.
  184.      * @param      pos the position at which to insert.
  185.      * @see        java.awt.TextComponent#setText
  186.      * @see        java.awt.TextArea#replaceRange
  187.      * @see        java.awt.TextArea#append
  188.      * @since      JDK1.1
  189.      */
  190.     public void insert(String str, int pos) {
  191.         insertText(str, pos);
  192.     }
  193.  
  194.     /**
  195.      * @deprecated As of JDK version 1.1,
  196.      * replaced by <code>insert(String, int)</code>.
  197.      */
  198.     public synchronized void insertText(String str, int pos) {
  199.     TextAreaPeer peer = (TextAreaPeer)this.peer;
  200.     if (peer != null) {
  201.         peer.insertText(str, pos);
  202.     } else {
  203.         text = text.substring(0, pos) + str + text.substring(pos);
  204.     }
  205.     }
  206.  
  207.     /**
  208.      * Appends the given text to the text area's current text.
  209.      * @param     str the text to append.
  210.      * @see       java.awt.TextArea#insert
  211.      */
  212.     public void append(String str) {
  213.         appendText(str);
  214.     }
  215.  
  216.     /**
  217.      * @deprecated As of JDK version 1.1,
  218.      * replaced by <code>append(String)</code>.
  219.      */
  220.     public synchronized void appendText(String str) {
  221.     if (peer != null) {
  222.         insertText(str, getText().length());
  223.     } else {
  224.         text = text + str;
  225.     }
  226.     }
  227.  
  228.     /**
  229.      * Replaces text between the indicated start and end positions 
  230.      * with the specified replacement text.
  231.      * @param     str      the text to use as the replacement.
  232.      * @param     start    the start position.
  233.      * @param     end      the end position.
  234.      * @see       java.awt.TextArea#insert
  235.      * @since     JDK1.1
  236.      */
  237.     public void replaceRange(String str, int start, int end) {
  238.     replaceText(str, start, end);
  239.     }
  240.  
  241.     /**
  242.      * @deprecated As of JDK version 1.1,
  243.      * replaced by <code>replaceRange(String, int, int)</code>.
  244.      */
  245.     public synchronized void replaceText(String str, int start, int end) {
  246.     TextAreaPeer peer = (TextAreaPeer)this.peer;
  247.     if (peer != null) {
  248.         peer.replaceText(str, start, end);
  249.     } else {
  250.         text = text.substring(0, start) + str + text.substring(end);
  251.     }
  252.     }
  253.  
  254.     /**
  255.      * Gets the number of rows in the text area.
  256.      * @return    the number of rows in the text area.
  257.      * @see       java.awt.TextArea#setRows
  258.      * @see       java.awt.TextArea#getColumns
  259.      * @since     JDK1
  260.      */
  261.     public int getRows() {
  262.     return rows;
  263.     }
  264.  
  265.     /**
  266.      * Sets the number of rows for this text area.
  267.      * @param       rows   the number of rows.
  268.      * @see         java.awt.TextArea#getRows
  269.      * @see         java.awt.TextArea#setColumns
  270.      * @exception   IllegalArgumentException if the value supplied
  271.      *                  for <code>rows</code> is less than zero.
  272.      * @since       JDK1.1
  273.      */
  274.     public void setRows(int rows) {
  275.     int oldVal = this.rows;
  276.     if (rows < 0) {
  277.         throw new IllegalArgumentException("rows less than zero.");
  278.     }
  279.     if (rows != oldVal) {
  280.         this.rows = rows;
  281.         invalidate();
  282.     }
  283.     }
  284.  
  285.     /**
  286.      * Gets the number of columns in this text area.
  287.      * @return    the number of columns in the text area.
  288.      * @see       java.awt.TextArea#setColumns
  289.      * @see       java.awt.TextArea#getRows
  290.      * @since     JDK1.0
  291.      */
  292.     public int getColumns() {
  293.     return columns;
  294.     }
  295.  
  296.     /**
  297.      * Sets the number of columns for this text area.
  298.      * @param       columns   the number of columns.
  299.      * @see         java.awt.TextArea#getColumns
  300.      * @see         java.awt.TextArea#setRows
  301.      * @exception   IllegalArgumentException if the value supplied
  302.      *                  for <code>columns</code> is less than zero.
  303.      * @since       JDK1.1
  304.      */
  305.     public void setColumns(int columns) {
  306.     int oldVal = this.columns;
  307.     if (columns < 0) {
  308.         throw new IllegalArgumentException("columns less than zero.");
  309.     }
  310.     if (columns != oldVal) {
  311.         this.columns = columns;
  312.         invalidate();
  313.     }
  314.     }
  315.  
  316.     /**
  317.      * Gets an enumerated value that indicates which scroll bars
  318.      * the text area uses. 
  319.      * <p>
  320.      * The <code>TextArea</code> class defines four integer constants 
  321.      * that are used to specify which scroll bars are available. 
  322.      * <code>TextArea</code> has one constructor that gives the 
  323.      * application discretion over scroll bars.
  324.      * @return     an integer that indicates which scroll bars are used.
  325.      * @see        java.awt.TextArea#SCROLLBARS_BOTH 
  326.      * @see        java.awt.TextArea#SCROLLBARS_VERTICAL_ONLY
  327.      * @see        java.awt.TextArea#SCROLLBARS_HORIZONTAL_ONLY
  328.      * @see        java.awt.TextArea#SCROLLBARS_NEITHER
  329.      * @see        java.awt.TextArea#TextArea(java.lang.String, int, int, int)
  330.      * @since      JDK1.1
  331.      */
  332.     public int getScrollbarVisibility() {
  333.         return scrollbarVisibility;
  334.     }
  335.  
  336.  
  337.     /**
  338.      * Determines the preferred size of a text area with the specified 
  339.      * number of rows and columns. 
  340.      * @param     rows   the number of rows.
  341.      * @param     cols   the number of columns.
  342.      * @return    the preferred dimensions required to display 
  343.      *                       the text area with the specified 
  344.      *                       number of rows and columns.
  345.      * @see       java.awt.Component#getPreferredSize
  346.      * @since     JDK1.1
  347.      */
  348.     public Dimension getPreferredSize(int rows, int columns) {
  349.         return preferredSize(rows, columns);
  350.     }
  351.  
  352.     /**
  353.      * @deprecated As of JDK version 1.1,
  354.      * replaced by <code>getPreferredSize(int, int)</code>.
  355.      */
  356.     public Dimension preferredSize(int rows, int columns) {
  357.         synchronized (getTreeLock()) {
  358.         TextAreaPeer peer = (TextAreaPeer)this.peer;
  359.         return (peer != null) ? 
  360.                peer.preferredSize(rows, columns) :
  361.                super.preferredSize();
  362.         }
  363.     }
  364.  
  365.     /**
  366.      * Determines the preferred size of this text area.  
  367.      * @return    the preferred dimensions needed for this text area.
  368.      * @see       java.awt.Component#getPreferredSize
  369.      * @since     JDK1.1
  370.      */
  371.     public Dimension getPreferredSize() {
  372.     return preferredSize();
  373.     }
  374.  
  375.     /**
  376.      * @deprecated As of JDK version 1.1,
  377.      * replaced by <code>getPreferredSize()</code>.
  378.      */
  379.     public Dimension preferredSize() {
  380.         synchronized (getTreeLock()) {
  381.         return ((rows > 0) && (columns > 0)) ? 
  382.                preferredSize(rows, columns) :
  383.                super.preferredSize();
  384.         }
  385.     }
  386.  
  387.     /**
  388.      * Determines the minimum size of a text area with the specified 
  389.      * number of rows and columns.
  390.      * @param     rows   the number of rows.
  391.      * @param     cols   the number of columns.
  392.      * @return    the minimum dimensions required to display 
  393.      *                       the text area with the specified 
  394.      *                       number of rows and columns.
  395.      * @see       java.awt.Component#getMinimumSize
  396.      * @since     JDK1.1
  397.      */
  398.     public Dimension getMinimumSize(int rows, int columns) {
  399.         return minimumSize(rows, columns);
  400.     }
  401.  
  402.     /**
  403.      * @deprecated As of JDK version 1.1,
  404.      * replaced by <code>getMinimumSize(int, int)</code>.
  405.      */
  406.     public Dimension minimumSize(int rows, int columns) {
  407.         synchronized (getTreeLock()) {
  408.         TextAreaPeer peer = (TextAreaPeer)this.peer;
  409.         return (peer != null) ? 
  410.                peer.minimumSize(rows, columns) :
  411.                super.minimumSize();
  412.         }
  413.     }
  414.  
  415.     /**
  416.      * Determines the minimum size of this text area. 
  417.      * @return    the preferred dimensions needed for this text area.
  418.      * @see       java.awt.Component#getPreferredSize
  419.      * @since     JDK1.1
  420.      */
  421.     public Dimension getMinimumSize() {
  422.     return minimumSize();
  423.     }
  424.  
  425.     /**
  426.      * @deprecated As of JDK version 1.1,
  427.      * replaced by <code>getMinimumSize()</code>.
  428.      */
  429.     public Dimension minimumSize() {
  430.         synchronized (getTreeLock()) {
  431.         return ((rows > 0) && (columns > 0)) ? 
  432.                minimumSize(rows, columns) :
  433.                super.minimumSize();
  434.         }
  435.     }
  436.  
  437.     /**
  438.      * Returns the parameter string representing the state of
  439.      * this text area. This string is useful for debugging.
  440.      * @return      the parameter string of this text area.
  441.      * @since       JDK1.0
  442.      */
  443.     protected String paramString() {
  444.     String sbVisStr;
  445.     switch (scrollbarVisibility) {
  446.         case SCROLLBARS_BOTH:
  447.         sbVisStr = "both";
  448.         break;
  449.         case SCROLLBARS_VERTICAL_ONLY:
  450.         sbVisStr = "vertical-only";
  451.         break;
  452.         case SCROLLBARS_HORIZONTAL_ONLY:
  453.         sbVisStr = "horizontal-only";
  454.         break;
  455.         case SCROLLBARS_NONE:
  456.         sbVisStr = "none";
  457.         break;
  458.         default:
  459.         sbVisStr = "invalid display policy";
  460.     }
  461.       
  462.     return super.paramString() + ",rows=" + rows + 
  463.         ",columns=" + columns + 
  464.       ", scrollbarVisibility=" + sbVisStr;
  465.     }
  466.  
  467. }
  468.